/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package pfc.arbol;

import java.io.File;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.tree.DefaultMutableTreeNode;
import org.apache.commons.io.monitor.FileAlterationListenerAdaptor;
import org.apache.commons.io.monitor.FileAlterationMonitor;
import org.apache.commons.io.monitor.FileAlterationObserver;
import pfc.comun.Proyecto;

/**
 *
 * @author xandru
 */
public class Monitor  extends FileAlterationListenerAdaptor {
    FileAlterationMonitor monitor = new FileAlterationMonitor(5000);
    FileAlterationObserver observer;
    Proyecto proyecto;
    FileTreeNode directoryListener;
    FileTreeNode fileListener;

    public Monitor(Proyecto p)  {
        proyecto = p;
        observer = new FileAlterationObserver(p.getPath());
        System.out.println("Monitorizando " + observer.getDirectory().getAbsolutePath());
        monitor.addObserver(observer);
        observer.addListener(this);
        
        try {
            monitor.start();
        } catch (Exception ex) {
            Logger.getLogger(Monitor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    Monitor(FileTreeNode nApp, String path) {
        observer = new FileAlterationObserver( path );
        System.out.println("Monitorizando " + observer.getDirectory().getAbsolutePath());
        monitor.addObserver(observer);
        observer.addListener(this);

        try {
            monitor.start();
        } catch (Exception ex) {
            Logger.getLogger(Monitor.class.getName()).log(Level.SEVERE, null, ex);
        }
    }


    @Override
    public void onDirectoryCreate(File file) {
        if (directoryListener!=null)
            directoryListener.directoryCreated(file);
    }

    @Override
    public void onDirectoryChange(File file) {
    }

    @Override
    public void onDirectoryDelete(File file) {
        if (directoryListener!=null)
        directoryListener.directoryDeleted(file);
    }

    @Override
    public void onFileCreate(File file) {
        if (fileListener!=null)
        fileListener.fileCreated(file);
    }

    @Override
    public void onFileChange(File file) {
    }

    @Override
    public void onFileDelete(File file) {
        if (fileListener!=null)
        fileListener.fileDeleted(file);
    }

    void addDirectoryListener(FileTreeNode nodo) {
        directoryListener = nodo;
    }

    void addFileListener(FileTreeNode nodo) {
        fileListener = nodo;
    }


}
